// marking/luckyDraw/index.js
const app = getApp()
var diyad = app.requirejs('biz/ad');
var core = app.requirejs('core');
const api = require("../../utils/api.js")
//计数器
var interval = null;

//值越大旋转时间越长  即旋转速度
var intime = 50;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: ['https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/blue.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg'],
    clickLuck: 'clickLuck',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let activeId = ''
    if (options.id) {

      wx.setStorageSync("activeId", options.id)
      activeId = options.id
    } else {
      activeId = wx.getStorageSync("activeId")
    }
    this.setData({
      activeId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let $this = this
    let apicontent = api.markings.selActivaty()
    core.post(apicontent, {
      id: $this.data.activeId
    }, function(res) {
      if (res.code == 1) {
        let position = [res.data.position]
        $this.setData({
          backageImg: res.data.backageImg,
          // activeId: res.data.id,
          position,
          nowinImg: res.data.nowinImg
        })
      } else if (res.code == 3) {

        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false,
          success(res) {
            wx.navigateBack({
              delta: 1,
            })
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false,
          success(res) {
            $this.setData({
              clickLuck: ""
            })
          }
        })
      }

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  navigate(e) {
    let url = e.currentTarget.dataset.url + "?activeId=" + this.data.activeId
    wx.navigateTo({
      url,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //点击抽奖按钮
  clickLuck: function(e) {

    let $this = this;
    let formId = e.detail.formId

    //设置按钮不可点击
    $this.setData({
      clickLuck: '',
    })
    let apicontent = api.markings.selActivatyApplet()
    core.post(apicontent, {
      id: $this.data.activeId,
      formId
    }, function(res) {
      if (res.code == 1) {
        if (res.data.isWinn == 0) {
          let arr = [1, 2, 3, 4, 5, 6, 7, 8]
          let win = res.data.Winn
          for (let i in win) {
            let index = arr.indexOf(win[i]);

            if (index > -1) {
              arr.splice(index, 1);
            }
          }
          let luckPosition = arr[Math.floor((Math.random() * arr.length))]
          $this.setData({
            winn: res.data.Winn,
            NoWinn: res.data.NoWinn,
            luckPosition: luckPosition - 1,
            luckImg: res.data.NoWinn.nowinImg,
            zhongjiang: false,
          })
        } else {


          $this.setData({
            winn: res.data.Winn,
            luckPosition: res.data.Winn.location - 1,
            luckImg: res.data.Winn.img,
            zhongjiang: true,
          })
        }
        $this.clfunction()


      } else {
        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false,
          success: function(res) {
            $this.clear()
          },
        })
        // $this.setData({
        //   nochance: true
        // })
        // setTimeout(function() {
        // $this.clear()
        //   $this.setData({
        //     nochance: false
        //   })
        // }, 1000);
      }
    })
  },
  //抽奖函数
  clfunction() {
    let $this = this;
    //设置按钮不可点击
    $this.setData({
      btnconfirm: 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/dianjichoujiangd.png',
      clickLuck: '',
    })
    //清空计时器
    clearInterval(interval);
    var index = 0;
    //循环设置每一项的切换图
    interval = setInterval(function() {
      if (index > 7) {
        index = 0;
        $this.data.images[7] = "https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg"
      } else if (index != 0) {
        $this.data.images[index - 1] = "https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg"
      }
      $this.data.images[index] = "https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/blue.jpg"
      $this.setData({
        images: $this.data.images,
      })
      index++;
    }, intime);
    //模拟网络请求时间  设为两秒   
    var stoptime = 2000;
    setTimeout(function() {
      $this.stop($this.data.luckPosition);
    }, stoptime)

  },
  //也可以写成点击按钮停止抽奖
  // clickStop:function(){
  //   var stoptime = 2000;
  //   setTimeout(function () {
  //   $this.stop(1);
  //   }, stoptime)
  // },
  stop: function(which) {
    var $this = this;
    //清空计数器
    clearInterval(interval);
    //初始化当前位置
    var current = -1;
    var images = $this.data.images;
    for (var i = 0; i < images.length; i++) {
      if (images[i] == "https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg") {
        current = i;
      }
    }
    //下标从1开始
    var index = current + 1;

    $this.stopLuck(which, index, intime, 10);
  },
  stopLuck: function(which, index, time, splittime) {
    var $this = this;
    //值越大出现中奖结果后减速时间越长
    let images = $this.data.images
    setTimeout(function() {
      //重置前一个位置
      if (index > 7) {
        index = 0;
        images[7] = "https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg"
      } else if (index != 0) {
        images[index - 1] = "https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg"
      }
      //当前位置为选中状态
      images[index] = "https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/blue.jpg"
      $this.setData({
        images: images,
      })
      //如果旋转时间过短或者当前位置不等于中奖位置则递归执行
      //直到旋转至中奖位置
      if (time < 400 || index != which) {
        //越来越慢
        splittime++;
        time += splittime;
        //当前位置+1
        index++;
        $this.stopLuck(which, index, time, splittime);
      } else {
        images[index] = $this.data.luckImg
        $this.setData({
          images: images
        })
        // btnconfirm
        //1秒后显示弹窗
        setTimeout(function() {
          $this.setData({
            boxs: true
          })
          if ($this.data.zhongjiang == true) {
            //中奖

            // wx.showModal({
            //   title: '提示',
            //   content: '恭喜中奖',
            //   showCancel: false,
            //   success: function(res) {
            //     if (res.confirm) {
            //       //设置按钮可以点击
            //       images[index] = 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg'
            //       images[0] = 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/blue.jpg'
            //       $this.setData({
            //         images,
            //         btnconfirm: $this.data.luckImg,
            //         clickLuck: 'clickLuck',
            //       })
            //       // $this.loadAnimation();
            //     }
            //   }
            // })
          } else {
            //中奖
            // wx.showModal({
            //   title: '提示',
            //   content: '很遗憾未中奖',
            //   showCancel: false,
            //   success: function(res) {
            //     if (res.confirm) {
            //       //设置按钮可以点击
            //       images[index] = 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg'
            //       images[0] = 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/blue.jpg'
            //       $this.setData({
            //         images,
            //         btnconfirm: $this.data.luckImg,
            //         clickLuck: 'clickLuck',
            //       })
            //       // $this.loadAnimation();
            //     }
            //   }
            // })
          }
        }, 1000);
      }
    }, time);

  },
  //清除中奖状态
  clear() {
    this.setData({
      boxs: false,
      zhongjiang: false,
      clickLuck: "clickLuck",
      images: ['https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/blue.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg', 'https://hrecminiprogramstorage.blob.core.chinacloudapi.cn/test-mini/choujiang/item.jpg'],
    })
  },
  //跳转到优惠券页面
  jump() {
    wx.navigateTo({
      url: '/pages/member/coupon/index',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
})